/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.raster.wms;

import java.awt.Image;
import java.io.IOException;

import chunmap.data.feature.Raster;
import chunmap.model.elem.Envelope;
import chunmap.util.image.WebClientUtil;

/**
 * @author yangjiandong
 * 
 */
public class WmsDataSource {

	private String url;
	private String version="1.1.1";

	/**
	 * @param url
	 * @param width
	 * @param height
	 */
	public WmsDataSource(String url) {
		super();
		this.url = url;
	}

	public Raster getImage(Envelope envelop, int width, int height) throws IOException {
		String parameters = url + "SERVICE=WMS&VERSION=" + version
				+ "&REQUEST=GetMap&BBOX=" + envelop2String(envelop)
				+ "&FORMAT=image/jpg"
				+ "&WIDTH=" + width + "&HEIGHT=" + height;
		
        Image image=WebClientUtil.requestImage(parameters);
        
        WmsImage raster=new WmsImage();
        raster.setImage(image);
        raster.setEnvelop(envelop);
        
		return raster;
	}

	private String envelop2String(Envelope env) {
		return "" + env.getMinX() + "," + env.getMinY() + "," + env.getMaxX()
				+ "," + env.getMaxY();
	}

	public Envelope getEnvelop() {
		// TODO wms envelope
		return new Envelope(92.37175137842411, 31.01897163866643,
				108.8716402641172, 44.2188827472209);
	}

	// ---------------------------------------------------------------
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
}